import requests

# https://search.jd.com/Search?keyword=%E7%81%AB%E8%85%BF%E8%82%A0&qrst=1&wq=%E7%81%AB%E8%85%BF%E8%82%A0&stock=1&page=7&s=177&click=0
# https://search.jd.com/Search?keyword=%E6%96%B9%E4%BE%BF%E9%9D%A2&stock=1&page=3
from bs4 import BeautifulSoup

base_url = "https://search.jd.com/Search"
headers={
    "Host": "search.jd.com",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"
}
params={
    "enc":"utf-8",
    "keyword":"",
    "page":""
}


def search(keyword,page):
    page = 1
    params['keyword'] = keyword
    params['page'] = page
    responseBody = requests.get(url=base_url,headers=headers,params=params)
    soup = BeautifulSoup(responseBody.content.decode(encoding='utf-8'),features="lxml")
    goods_list = soup.select(".gl-item")
    resultGoodsList = []
    try:
        for goods in goods_list:
            gds={}
            gds['img'] = goods.select('img')[0]['data-lazy-img']
            gds['price'] = goods.select('.p-price')[0].select('i')[0].string
            p_name = goods.select('.p-name')[0]
            gds['title'] = p_name.select('a')[0]['title']
            gds['href'] = p_name.select('a')[0]['href']
            gds['desc'] = p_name.select('em')[0].text
            resultGoodsList.append(gds)
    except Exception as err:
        print(err)
    return resultGoodsList

def fetch_data_from_jindon(keword,num):
    count = 0
    page = 1
    result = []
    while count<num:
        goodsList = search(keword,page)
        size = len(goodsList)
        if size<=0:
            return result
        count+=size
        result+=goodsList
    return result

if __name__ == '__main__':
    result = fetch_data_from_jindon("vue",10)
    for r in result:
        print(r)